﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetDon.OA.Data.CollectionClasses;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.HelperClasses;
using NetDon.OA.Data.Linq;
using NetDon.OA.Interface;
using NetDon.OA.Interface.Common;
using NetDon.Utilities;

namespace NetDon.OA.Business
{
    public class VehicleModelService :IVehicleModelService
    {
        public bool AddVechicleModel(VehicleModelData vechicleModelData)
        {
            var entity = ConvertToEntityByBusinessData(vechicleModelData, true);
            return entity.Save();
        }

        public bool ModifyVechicleModel(VehicleModelData vechicleModelData)
        {
            var entity = ConvertToEntityByBusinessData(vechicleModelData, false);
            return entity.Save();
        }

        public bool RemoveVechicleModel(Guid vehicleModeId)
        {
            var attachmentsCollection = new VehicleModelCollection();
            int result = attachmentsCollection.DeleteMulti(VehicleModelFields.Id == vehicleModeId);
            return result > 0;
        }

        public VehicleModelData GetVechicleModelById(Guid vehicleModeId)
        {
            var meta = new LinqMetaData();
            var linqResult = (from q in meta.VehicleModel
                              where q.Id == vehicleModeId
                              select q).FirstOrDefault();

            if (linqResult != null)
            {
                return ConvertToBusinessDataByEntity(linqResult);
            }

            return null;
        }

        public List<VehicleModelData> GetVechicleModels()
        {
            var meta = new LinqMetaData();
            var listResult = from q in meta.VehicleModel
                          select q;
            List<VehicleModelData> listAttachments = new List<VehicleModelData>();
            if (listResult != null && listResult.Count() > 0)
            {
                foreach (var attachmentsEntity in listResult)
                {
                    var obj = ConvertToBusinessDataByEntity(attachmentsEntity);
                    if (obj != null)
                    {
                        listAttachments.Add(obj);
                    }
                }
            }

            return listAttachments;
        }

        private VehicleModelEntity ConvertToEntityByBusinessData(VehicleModelData vechicleModel, bool isAdd)
        {
            VehicleModelEntity entity = null;
            if (isAdd)
            {
                entity = new VehicleModelEntity();
                entity.Id = vechicleModel.Id;
                if (entity.Id == Guid.Empty)
                {
                    entity.Id = Guid.NewGuid();
                }
            }
            else
            {
                entity = new VehicleModelEntity(vechicleModel.Id);
            }
            entity.Width = vechicleModel.Width;
            entity.Height = vechicleModel.Height;
            entity.Length = vechicleModel.Length;
            entity.ModelDescription = vechicleModel.ModelDescription;
            entity.ModelName = vechicleModel.ModelName;
            entity.AreaId = vechicleModel.AreaId;

            return entity;
        }

        private VehicleModelData ConvertToBusinessDataByEntity(VehicleModelEntity vehicleModelEntity)
        {
            return new VehicleModelData
            {
                Id = vehicleModelEntity.Id,
                Width = vehicleModelEntity.Width,
                Height = vehicleModelEntity.Height,
                Length = vehicleModelEntity.Length,
                ModelName = vehicleModelEntity.ModelName,
                ModelDescription = vehicleModelEntity.ModelDescription,
                AreaId = vehicleModelEntity.AreaId
            };
        }


        public bool CheckVechicleModelReferenced(Guid vehicleModeId)
        {
            var meta = new LinqMetaData();
            var linqResult = (from q in meta.IssuingOrderVehicleInfo
                              where q.VechicleModel == vehicleModeId
                              select q).FirstOrDefault();
            return linqResult != null;
        }
    }
}
